/**
 * @(#) ActuatorValue.java
 */

package pcsr.hwVirtualization;

import pcsr.Debug;

/**
 * Value of an actuator managed by PCSR.
 * @author Paulo Fagundes
 *
 */
public class ActuatorValue
{
	private Object extendedData;
	
	private int referential;
	
	private double value;

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object obj)
	{
		if(obj == null )
		{
			return false;
		}
		
		if(!(obj instanceof ActuatorValue) )
		{
			return false;
		}
		
		ActuatorValue avObj = (ActuatorValue)obj;		
		if(
				value != avObj.value ||
				referential != avObj.referential ||
				extendedData != avObj.extendedData
			)
		{
			return false;
		}
		
		return true;
	}
	
	/**
	 * Gets the extended data.
	 * @return The extendedData
	 */
	public Object GetExtendedData( ) {
		return extendedData;
	}
	
	/**
	 * Gets the real value of the actuator.
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p> 
	 * @return The real value of the actuator.
	 */
	public double GetUnnormalizedValue() {
		if(Debug.DEBUG)
		{
			System.err.println("value:"+value+" Referencial:"+referential);
		}
		return value * referential;
	}

	/**
	 * Gets the relative value of the actuator.
	 * @return The actuator value
	 */
	public double GetValue( ) {
		return value;
	}

	/**
	 * Sets extended data.
	 * @param extendedData The extendedData to set
	 */
	public void SetExtendedData( Object extendedData ) {
		this.extendedData = extendedData;
	}

	/**
	 * Sets the absolute value of the actuator. 
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p>
	 * @param actuatorValue The actuatorValue to set
	 */
	public void SetUnnormalizedValue( double actuatorValue ) {
		if(Math.abs(actuatorValue) >= referential)
		{
			this.value = 1;
		}
		else
		{
			this.value = actuatorValue / referential;
		}
	}

	/**
	 * Sets the relative value of the actuator.
	 * @param actuatorValue The value to set
	 */
	public void SetValue( double actuatorValue ) {
		if(Math.abs(actuatorValue) >= 1)
		{
			this.value = 1;
		}
		else
		{
			this.value = actuatorValue;
		}
	}

	/**
	 * Gets the referential value.
	 * <p><strong>Remarks:</strong> This method is for</p> 
	 * @return The referential
	 */
	int GetReferential( ) {
		return referential;
	}
	
	/**
	 * Sets the referential value. This value is used to calculate relative values.
	 * <p><strong>Remarks:</strong> This method is for driver use only.</p>
	 * @param referential The referential to set
	 */
	void SetReferential( int referential ) {
		this.referential = referential;
	}

	/**
	 * ActuatorValue constructor.
	 * @param maxValue The maximum real value that actuator can reach.
	 */
	public ActuatorValue(int maxValue)
	{
		referential = maxValue;
	}
}
